package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.dao.McpCarDealerInformationDao;
import com.pgytesting.secondhandcar.dao.McpUserInfoDao;
import com.pgytesting.secondhandcar.dao.OrderNameDao;
import com.pgytesting.secondhandcar.dao.VehicleInformationDao;
import com.pgytesting.secondhandcar.model.OrderName;
import com.pgytesting.secondhandcar.model.VehicleInformation;
import com.pgytesting.secondhandcar.service.VehicleInformationService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆信息(VehicleInformation)表服务实现类
 *
 * @author makejava
 * @since 2019-08-12 18:55:58
 */
@Slf4j
@Service("vehicleInformationService")
public class VehicleInformationServiceImpl implements VehicleInformationService {
    @Resource
    private VehicleInformationDao vehicleInformationDao;

    @Resource
    private OrderNameDao orderNameDao;

    @Resource
    McpUserInfoDao mcpUserInfoDao;

    @Resource
    McpCarDealerInformationDao mcpCarDealerInformationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public VehicleInformation queryById(String id) {
        return this.vehicleInformationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<VehicleInformation> queryAllByLimit(int offset, int limit) {
        return this.vehicleInformationDao.queryAllByLimit(offset, limit);
    }

    /**
     * 条件查询多条数据
     *
     * @param vehicleInformation 实例对象
     * @return 对象列表
     */
    @Override
    public List<VehicleInformation> queryAll(VehicleInformation vehicleInformation) {
        return this.vehicleInformationDao.queryAll(vehicleInformation);
    }

    /**
     * 新增数据
     *
     * @param vehicleInformation 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseMessage insert(VehicleInformation vehicleInformation) {
        return ResponseMessage.errorResponse("暂未实现");
    }

    /**
     * 修改数据
     *
     * @param vehicleInformation 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public ResponseMessage update(VehicleInformation vehicleInformation) {
        // 获取到订单号
        String orderId = vehicleInformation.getId();
        OrderName orderName = orderNameDao.queryById(orderId);
        // 进行对象转换，防止修改其他数据
        VehicleInformation vehicleInf = new VehicleInformation();
        vehicleInf.setId(orderName.getVehicleInformationId());
        vehicleInf.setVehicleColor(vehicleInformation.getVehicleColor());
        vehicleInf.setVehicleGear(vehicleInformation.getVehicleGear());
        vehicleInf.setFirstTimeOnTheCard(vehicleInformation.getFirstTimeOnTheCard());
        vehicleInf.setKilometers(vehicleInformation.getKilometers());
        vehicleInf.setFrameNumber(vehicleInformation.getFrameNumber());
        vehicleInf.setUpdatedBy(SessionUtil.getSessionMcpCar().getPhoneNumber());
        this.vehicleInformationDao.update(vehicleInf);
        // 更新订单信息
        OrderName orderName1 = new OrderName();
        orderName1.setId(orderId);
        orderName1.setOrderStatus(Parameters.ORDER_22001);
        orderName1.setUpdatedBy(SessionUtil.getSessionMcpCar().getPhoneNumber());
        orderNameDao.update(orderName1);
        Map<String, String> map = new HashMap<>();
        map.put("orderId", orderId);
        return ResponseMessage.successResponse(map, "车辆信息更新成功");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.vehicleInformationDao.deleteById(id) > 0;
    }

    @Override
    public Map<String, Object> queryVehicleDetail(VehicleInformation vehicleInformation) {
        return vehicleInformationDao.queryVehicleDetail(vehicleInformation);
    }

	@Override
	public ResponseMessage queryVehicleByOrderId(OrderName orderName) {
		
		Map<String,Object> map = orderNameDao.queryUserVehicleInformation(orderName);
		return ResponseMessage.successResponse(map);
	}
}